package cn.sdormitory.job;

import cn.sdormitory.smartdor.dao.SdRentDao;
import cn.sdormitory.smartdor.dao.SdRentPayRecordDao;
import cn.sdormitory.smartdor.dto.SdRentExportDTO;
import cn.sdormitory.smartdor.entity.SdRent;
import cn.sdormitory.smartdor.entity.SdRentPayRecord;
import cn.sdormitory.smartdor.vo.req.SdRentQueryParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class CheckNextPayDateScheduled {

    @Autowired
    SdRentDao sdRentDao;

    @Autowired
    SdRentPayRecordDao sdRentPayRecordDaoDao;


    /**
     * 更新学生住宿费表状态
     *
     */
//    @Scheduled(cron = "0 15 10 * * ?")
    public void updateSdRentPayStatus(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        List<SdRentExportDTO> unExpiredSdRentList = sdRentDao.findUnExpiredSdRentList();
        for(SdRentExportDTO item : unExpiredSdRentList){
            try {
                if(item == null){
                    continue;
                }
                if(item.getNextPayDate() == null){
                    continue;
                }
                if(df.parse(item.getNextPayDate()).before(new Date())){
                    LambdaQueryWrapper<SdRent> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(SdRent::getStudentNo,item.getStudentNo());
                    SdRent sdRent = sdRentDao.selectOne(wrapper);
                    sdRent.setPayStatus("2");
                    sdRentDao.updateById(sdRent);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
    }
}
